package string;

public class StringDemo2 {
	public static void main(String[] args) {
		//构造方法
		String s1 = new String("hello worlod");
		char[] arr = {'a','b','c'};
		// 由一个字符数组构造
		String s2 = new String(arr);  //"abc" 
		String s4 = new String(arr,1, 2);  //"bc" 从arr数组的下标1位置要2位
		// 由一个byte数组构造,把每个byte转成char
		byte[] arr2 = {97,98,99};
		String s3 = new String(arr2); //"abc"
		System.out.println(s1);  // 返回字面量,所以String重写了toString()方法
		
		//1.求长度int
		System.out.println(s1.length());
		//2.求某个位置上的字符char  !!!下标不要越界
		System.out.println(s1.charAt(1));
		//3.求某个字符char第一次出现的下标int,如果没找到返回-1
		System.out.println(s1.indexOf('l')); //2
		System.out.println(s1.indexOf('l', 3)); // 从下标3开始找'l'
		System.out.println(s1.indexOf("lo"));  // 返回的是l的下标
		System.out.println(s1.indexOf("lo",5));  // 从下标5开始找"lo"
		System.out.println(s1.lastIndexOf('o')); //从后往前找，下标也是从前面数
		//4.比较是否相等   boolean
		System.out.println("hello".equals("HELLO"));  //false 区分大小写
		System.out.println("hello".equalsIgnoreCase("HELLO")); //true 忽略大小写
		//5.比较字符串大小  int 对象大返回正，参数大返回负，一样返回0
		System.out.println("zabcdefg".compareTo("Zabcdefg")); // 区分大小写
		System.out.println("zabcdefg".compareToIgnoreCase("Zabcdefg")); // 忽略大小写
		//6.判断字符串是否为空，为空返回true
		System.out.println("".isEmpty());
		//7.判断是否包含,包含就返回true
		System.out.println("hello world".contains("o w"));
		//8.判断是否以某字符串开头/结尾
		System.out.println("hello".startsWith("hello")); //true
		System.out.println("world".endsWith("w"));       //false
		//9.转换大小写 返回转换后新的字符串
		System.out.println("hello".toUpperCase());
		System.out.println("HELLO".toLowerCase());
		//10.截取子串 
		System.out.println("hello world".substring(3)); //从开始一直截取到最后
		System.out.println("hello world".substring(3, 10)); //截取下标从3-9之间的，不包括下标10
		//11.切割字符串 String[]
		String[] split = "123,456,789".split(",");
		for(String s : split) { 
			System.out.println(s);
		}
		//12.去掉字符串两头的空白(中间空白不去掉)
		System.out.println("     h  e   llo    ".trim());
		//13.替换                              旧字符,新字符
		System.out.println("hello world".replace('l', 'L'));
		System.out.println("hello world".replace("llo", "LLO"));
		//正则表达式：找元素，替换，匹配某种格式
		//14.把字符串转换成char[],byte[]
		char[] charArray = "hello".toCharArray();
		byte[] bytes = "world".getBytes();
		//15.静态方法
		String join = String.join("", "a","b","c","d","afsaf","lkjor");
		System.out.println(join);
	}
}
